package com.easylinkin.linkappapi.inspection.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.inspection.dto.ElectricBoxDTO;
import com.easylinkin.linkappapi.inspection.entity.ElectricBox;
import com.easylinkin.linkappapi.inspection.entity.ElectricCheck;
import com.easylinkin.linkappapi.inspection.service.ElectricBoxService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogOperateType;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.security.service.LinkappUserService;
import com.easylinkin.sm.entity.User;
import com.easylinkin.sm.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 配电箱管理
 */
@RestController
@RequestMapping("electricBox")
@Api(tags = "配电箱管理")
public class ElectricBoxController {
  
  @Autowired
  private ElectricBoxService electricBoxService;

  @Autowired
  private LinkappUserService linkappUserService;

  /**
   * @Description: 增加配电箱
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加配电箱")
  public RestMessage insert(@RequestBody ElectricBox electricBox){
    electricBoxService.insert(electricBox);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除配电箱（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除配电箱（包含批量删除）")
  @CommonOperateLogAnnotate(module = LogModule.SAFE_MANAGEMENT, desc = "")
  public RestMessage delBatch(@RequestBody ElectricBox electricBox){
    Assert.notEmpty(electricBox.getIds(),"id不能为空");
    // 记录日志详情
    List<String> electricBoxs = new ArrayList<>();
    electricBox.getIds().forEach(p->{
      ElectricBox boxServiceById = electricBoxService.getById(p);
      electricBoxs.add(boxServiceById.getCode());
    });
    if(CollectionUtil.isNotEmpty(electricBoxs)){
      LogHelper
          .setContent(LogOperateType.SAFE_CHECK_BOX_DELETE, StringUtil.join(electricBoxs,","));
    }
    electricBoxService.delBatch(electricBox.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑配电箱
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑配电箱")
  @CommonOperateLogAnnotate(module = LogModule.SAFE_MANAGEMENT, desc = "")
  public RestMessage updateById(@RequestBody ElectricBox electricBox){
    Assert.notNull(electricBox.getId(),"id不能为空");
    // 记录日志详情
    ElectricBox old = electricBoxService.getById(electricBox.getId());
    LogHelper.setContent(LogOperateType.SAFE_CHECK_BOX_UPDATE,old.getCode());
    electricBoxService.updateOne(electricBox);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询配电箱详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询配电箱详情")
  public RestMessage findById(@PathVariable("id")String id) {
    Assert.notNull(id,"id不能为空");
    ElectricBox electricBox = electricBoxService.getById(id);
    try {
      String leadId = electricBox.getLeadId();
      if(StringUtils.isNotBlank(leadId)){
        LinkappUser user = linkappUserService.findById(Long.valueOf(leadId));
        if(user!=null){
          electricBox.setLeadName(StringUtils.isNotBlank(user.getNickname())?user.getNickname():user.getUsername());
        }
      }
    }catch (Exception e){
    }
    return RestBuilders.successBuilder().data(electricBox).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<ElectricBox> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<ElectricBox> record =  electricBoxService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }


//  /**
//   * @Description: 根据条件，分页查询 未被巡检过的
//   * @author tongjie
//   * @date 2022/07/11
//   */
//  @PostMapping("listUnCheckedDetails")
//  @ApiOperation("根据条件，分页查询 未被巡检过的")
//  public RestMessage listUnCheckedDetails(@RequestBody RequestModel<MessageCenter> requestModel){
//    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
//    Assert.notNull(requestModel.getPage(), "page 不能为空");
//    IPage<ElectricBox> record =  electricBoxService.listUnCheckedDetails(requestModel);
//    return RestBuilders.successBuilder().data(record).build();
//  }

  /**
   * @Description: 查询可选负责人列表
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("leads")
  @ApiOperation("查询可选负责人列表")
  public RestMessage queryLeads(){
    List<LinkappUser> users =  electricBoxService.queryLeads();
    return RestBuilders.successBuilder().data(users).build();
  }

  /**
   * 获取未绑定的电气火灾监控设备
   * @return
   */
  @GetMapping("getMonitorDevice/{id}")
  @ApiOperation("获取未绑定的电气火灾监控设备")
  public RestMessage getUnRefMonitorDevice(@PathVariable("id") String id){
    List<Device> devices =  electricBoxService.getMonitorDevice(id);
    return RestBuilders.successBuilder().data(devices).build();
  }


  /**
   * 查看当天的巡检记录
   * @param type 1已巡检 0未巡检
   */
  @PostMapping("todayCheck")
  @ApiOperation("查看当天的巡检记录")
  public RestMessage todayCheck(@RequestBody RequestModel<ElectricBoxDTO> requestModel){
    IPage<ElectricBoxDTO> record =  electricBoxService.todayCheck(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

